package com.botarena.server.servlets.rpc;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import com.botarena.client.bot.remote.BotAccess;
import com.botarena.server.helper.mappers.BotMapper;
import com.botarena.server.model.Account;
import com.botarena.server.model.Bot;
import com.botarena.server.model.Contest;
import com.botarena.server.services.AccountService;
import com.botarena.server.services.BotService;
import com.botarena.server.services.ContestService;
import com.botarena.shared.BotHomeInfo;
import com.botarena.shared.BotInfo;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class BotAccessServlet extends RemoteServiceServlet implements BotAccess {
	private static final Logger log = Logger.getLogger(BotAccessServlet.class.getName());

	private static final long serialVersionUID = 7380070798494474283L;
	private final BotService botService;
	private final AccountService accountService;
	private final ContestService contestService;

	public BotAccessServlet() {
		botService = new BotService();
		accountService = new AccountService();
		contestService = new ContestService();
	}
	
	@Override
	public List<BotInfo> getMyBots(final String contestKey) {
		List<BotInfo> result = null;
		
		try {
			Account account = accountService.getAccount(accountService.getGoogleUser());
			result = botService.getUserBots(account.getKey(), KeyFactory.stringToKey(contestKey));
			
		} catch (Exception e) {
			result = new ArrayList<BotInfo>();
		}
		
		return result;
	}

	@Override
	public BotInfo getByKey(String botKey) {
		Bot bot = botService.getByKey(KeyFactory.stringToKey(botKey));
		
		BotMapper mapper = new BotMapper();
		BotInfo botInfo = mapper.map(bot);
		
		Account account = accountService.getAccount(accountService.getGoogleUser());
		if (!account.getKey().equals(bot.getAuthor())) {
			botInfo.setSourceCode("");
			botInfo.setParameters("");
			botInfo.setCompilationResults("");
		}
		
		return botInfo;
	}
	
	public BotHomeInfo getHomeInfo(String botKey) {
		Bot bot = botService.getByKey(botKey);
		Account author = accountService.getByKey(bot.getAuthor());
		Contest contest = contestService.getByKey(bot.getContest());
		
		BotHomeInfo botHome = new BotHomeInfo();
		botHome.setKey(KeyFactory.keyToString(bot.getKey()));
		botHome.setName(bot.getName());
		botHome.setPoints(bot.getRank());
		botHome.setWins(bot.getWins());
		botHome.setLosses(bot.getLosses());
		botHome.setDraws(bot.getDraws());
		botHome.setContestant(bot.isContestant());
		
		botHome.setAuthorKey(KeyFactory.keyToString(bot.getAuthor()));
		botHome.setAuthorName(author.getName());
		
		botHome.setContestKey(KeyFactory.keyToString(bot.getContest()));
		botHome.setContestName(contest.getName());
		
		return botHome;
	}

	@Override
	public BotInfo updateBot(BotInfo botInfo) throws Exception {
		Bot bot = new Bot();
		bot.setName(botInfo.getName());
		bot.setKey(KeyFactory.stringToKey(botInfo.getKey()));
		bot.setParameters(botInfo.getParameters());
		bot.setContestant(botInfo.isContestant());
		
		Account account = accountService.getAccount(accountService.getGoogleUser());
		if (botService.isNameAvailable(bot) || !botService.isAuthor(account.getKey(), bot.getKey()))
			return null;
		
		Bot updated = botService.update(bot);
		BotMapper mapper = new BotMapper();
		return mapper.map(updated);
	}

	@Override
	public void deleteBot(final String botKey) {
		Bot bot = botService.getByKey((KeyFactory.stringToKey(botKey)));
		if (bot != null) {
			botService.delete(bot);
		}
	}
}